/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.vineatoum.view.component;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.RowFilter;

import java.awt.GridBagLayout;

import javax.swing.JLabel;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.vineatoum.business.OenologicalProduct;
import org.vineatoum.business.configuration.OenologicalProductType;
import org.vineatoum.business.configuration.ProducerOeno;
import org.vineatoum.view.utils.jtablecustom.JTableCustom;

import javax.swing.ListSelectionModel;

public class OenologicalProductPanel extends JPanel {
	private static final long serialVersionUID = -4870935964931177937L;
	
	private JTableCustom<OenologicalProduct> listProducts;
	private JTextField textFieldFilter;
	private JTableCustom<OenologicalProduct> tableProduct;

	/**
	 * Create the panel.
	 */
	public OenologicalProductPanel() {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JLabel lblProducts = new JLabel("Produits oenologiques");
		GridBagConstraints gbc_lblAntcdents = new GridBagConstraints();
		gbc_lblAntcdents.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblAntcdents.insets = new Insets(0, 0, 5, 5);
		gbc_lblAntcdents.gridx = 0;
		gbc_lblAntcdents.gridy = 0;
		add(lblProducts, gbc_lblAntcdents);
				
		List<OenologicalProduct> products = new ArrayList<OenologicalProduct>();
		for (Iterator<OenologicalProduct> iterator = OenologicalProduct.products.iterator(); iterator.hasNext();) {
			OenologicalProduct product = (OenologicalProduct) iterator.next();
			if(product.name != null && !product.name.isEmpty()) {
				products.add(product);
			}
		}
		
		
		listProducts = new JTableCustom<OenologicalProduct>(new OenologicalProduct());
		GridBagConstraints gbc_listProducts = new GridBagConstraints();
		gbc_listProducts.insets = new Insets(0, 0, 5, 5);
		gbc_listProducts.fill = GridBagConstraints.BOTH;
		gbc_listProducts.gridx = 0;
		gbc_listProducts.gridy = 1;
		
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(listProducts.getTableHeader(), BorderLayout.NORTH);
		panel.add(listProducts, BorderLayout.CENTER);
		add(panel, gbc_listProducts);
		    
	    OenologicalProduct product = new OenologicalProduct();
	    product.setName("PRODUCT");
	    product.setLotNumber("LOT243");
	    product.setProducer(ProducerOeno.producers.get(0));
	    product.setSubstanceType(OenologicalProductType.types.get(0));
	    listProducts.addRow(product);
	    
	    product = new OenologicalProduct();
	    product.setName("ABABABABA");
	    product.setLotNumber("LOT2673");
	    product.setProducer(ProducerOeno.producers.get(0));
	    product.setSubstanceType(OenologicalProductType.types.get(0));
	    listProducts.addRow(product);
	    
//	    listProducts.setAutoCreateRowSorter(true);
		
		tableProduct = new JTableCustom<OenologicalProduct>(new OenologicalProduct());
		tableProduct.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableProduct.setDragEnabled(true);
	
		GridBagConstraints gbc_lblTableProducts = new GridBagConstraints();
		gbc_lblTableProducts.fill = GridBagConstraints.BOTH;
		gbc_lblTableProducts.insets = new Insets(0, 0, 5, 5);
		gbc_lblTableProducts.gridx = 2;
		gbc_lblTableProducts.gridy = 1;
		add(tableProduct, gbc_lblTableProducts);
		
		
		textFieldFilter = new JTextField("Filter");
		GridBagConstraints gbc_lblSuccesseurs = new GridBagConstraints();
		gbc_lblSuccesseurs.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblSuccesseurs.insets = new Insets(0, 0, 5, 5);
		gbc_lblSuccesseurs.gridx = 0;
		gbc_lblSuccesseurs.gridy = 2;
		add(textFieldFilter, gbc_lblSuccesseurs);
		
	    textFieldFilter.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent e) {
				listProducts.getSorter().setRowFilter(RowFilter.regexFilter(textFieldFilter.getText()));
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
		});

	}
	
	public void setFilter(String filter) {
		textFieldFilter.setText(filter);
		listProducts.getSorter().setRowFilter(RowFilter.regexFilter(filter));
	}

}
